package com.dwxt.mall.admin.controller;

import com.dwxt.common.annotation.ValidateFiled;
import com.dwxt.common.annotation.ValidateGroup;
import com.dwxt.common.base.BaseResult;
import com.dwxt.mall.admin.service.ProductAttrServiceAdmin;
import com.dwxt.mall.entity.ProductCategory;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RequestMapping("/mall/attr")
@RestController
public class ProductAttrControllerAdmin {

    @Autowired
    private ProductAttrServiceAdmin productAttrServiceAdmin;

    //展示商品种类
    @RequestMapping("/selectProductCategoryPage")
    @ValidateGroup(fileds = {
            @ValidateFiled(index = 0, notNull = true),
            @ValidateFiled(index = 1, notNull = true),
    })
    public BaseResult selectProductCategoryPage(Integer pageNum, Integer pageSize, ProductCategory productCategory) {
        PageHelper.startPage(pageNum, pageSize);
        List<ProductCategory> productCategoryList = productAttrServiceAdmin.selectProductCategoryList(productCategory);
        PageInfo<ProductCategory> pageInfo = new PageInfo<>(productCategoryList);
        return BaseResult.ok(pageInfo);
    }

    //获取所有商品种类
    @RequestMapping("/selectAllProductCategory")
    public BaseResult selectAllProductCategory() {
        List<ProductCategory> productCategoryList = productAttrServiceAdmin.selectProductCategoryList(null);
        return BaseResult.ok(productCategoryList);
    }

    //查询module不为null的种类
    @RequestMapping("/selectModuleProductCategory")
    public BaseResult selectModuleProductCategory(String module) {
        List<ProductCategory> productCategoryList = productAttrServiceAdmin.selectModuleProductCategory(module);
        return BaseResult.ok(productCategoryList);
    }

    //新增或修改
    @RequestMapping("/createOrUpdate")
    @ValidateGroup(fileds = {
            @ValidateFiled(index = 0, notNull = true, filedName = "name"),
    })
    public BaseResult createOrUpdate(ProductCategory productCategory) {
        productAttrServiceAdmin.createOrUpdate(productCategory);
        return BaseResult.ok();
    }

    @RequestMapping("/delete")
    @ValidateGroup(fileds = {
            @ValidateFiled(index = 0, notNull = true),
    })
    public BaseResult delete(Integer id) {
        productAttrServiceAdmin.delete(id);
        return BaseResult.ok();
    }


    @RequestMapping("/onOrDown")
    public BaseResult onOrDown(Integer id) {
        productAttrServiceAdmin.onOrDown(id);
        return BaseResult.ok();
    }
}
